// pages/movies/movies.js
var util = require('../utils/util.js');
const request=require('../../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    movieName:"",
    movieTimes:"",
    //屏幕宽度/2
    left:1,
    seatArr:[],
    buy:"请先选座",
    opacity:0.2,
    seatFinish:[],
    num:0,
    price:10,
    modalHidden:true,
    day_id:0,
    cinema_id:0,
    hall_id:0,
    movie_id:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    let day_id=parseInt(options.day_id);
    let index=options.index;
    let cinemanm=options.cinemanm;
    this.setData({
      day_id,
      index,
      cinemanm
    });
    wx.setNavigationBarTitle({
      title: `${cinemanm}`
    })
    this.promoteseat();
    this.getmovieinfo();
  },

  //获取当前选择电影信息
  async getmovieinfo(){
    let movieinfo=await request.myrequest('/miniapp/getordermovieinfo',{day_id:this.data.day_id});//获取cinemaid movieid moviename

    this.setData({
      movieName:movieinfo.data[0].movie_name,
      cinema_id:movieinfo.data[0].cinema_id,
      movie_id:movieinfo.data[0].movie_id
    });
    let startinfo=await request.myrequest('/miniapp/getordermovietime',{day_id:this.data.day_id});

    this.setData({
      hall_id:startinfo.data[0].hall_id,
      movieTimes:startinfo.data[0].starttime,
      price:startinfo.data[0].price,
      day_id:startinfo.data[0].day_id
    });
    this.getorderedseat()
  },

// 获取已被选择的座位信息
  async getorderedseat(){
    //根据hall_id可以查找状态为已定的座位
    let orderedseatinfo=await request.myrequest('/miniapp/getorderedseat',{hall_id:this.data.hall_id});
    let orderedseat=orderedseatinfo.data;
    let seatArr=this.data.seatArr;
    for(let i=0;i<orderedseat.length;i++){
      seatArr[orderedseat[i].x_coord-1][orderedseat[i].y_coord-1]='../img/seat2.png';
    };
    this.setData({
      seatArr
    });
  },


// 座位循环
  promoteseat(){
    //座位循环
    var row = 8;
    var col = 8;
    var seatArr = new Array(row);   //row行
    var src = "../img/seat1.png";
    for(let i = 0;i<row;i++){
      seatArr[i] = new Array(col); 
      for(let j = 0;j<col;j++){
        seatArr[i][j] = src;
      }
    }
    this.setData({
      seatArr:seatArr
    })

  },



  seatChange:function(e){
    var that = this;
    var rowCol = e.currentTarget.dataset.index;

    var row = parseInt(rowCol.substring(0, 1))
    var col = parseInt(rowCol.substring(1, 2))
    var row1 = row + 1;//索引是从0 开始换算成行
    var col1 = col + 1;
    var arr = that.data.seatArr
    var num = 0;
    if (arr[row][col] == "../img/seat2.png"){
      var num = that.data.num;
      var seatFinish = that.data.seatFinish;
      --num;
      if(num <= 0){
        that.setData({
          opacity: 0.2,
          buy: "请先选座",
        })
      }
      arr[row][col] = "../img/seat1.png"
      seatFinish.splice(seatFinish.length-1,1)
      console.log(seatFinish)
      that.setData({
        seatArr: arr,
        num :num,
        seatFinish:seatFinish
      })
      console.log("您取消了" + row1 + "排" + col1 + "座")
    }else{
      var num = that.data.num;
      var seatFinish = that.data.seatFinish;
      ++num;
      if(num > 4){
          that.setData({
            modalHidden: false,
          })
          return false;
      }
      arr[row][col] = "../img/seat2.png"
      var seat = {
      row:row1,
      col:col1
    }
    seatFinish.push(seat)

    that.setData({
      opacity:1,
      buy:"点击购买",
      seatArr : arr,
      num : num,
      seatFinish: seatFinish
    })
    // console.log("您选择了" + row1 + "排" + col1 + "座")
    // console.log(arr)
    }
  },

  modalChange:function(){
    var that = this;
    that.setData({
      modalHidden: true,
    })
  },

  buy(e){
    let seatFinish=this.data.seatFinish;
    let movietimes=this.data.movieTimes;
    let moviename=this.data.movieName;
    let price=this.data.price;
    let day_id=this.data.day_id;
    let index=this.data.index;
    let movie_id=this.data.movie_id;
    let cinemanm=this.data.cinemanm;
    let hall_id=this.data.hall_id;
    seatFinish=JSON.stringify(seatFinish)
    wx.navigateTo({
      url: `../../buy/buy?seatFinish=${seatFinish}&&movietimes=${movietimes}&&moviename=${moviename}&&day_id=${day_id}&&price=${price}&&index=${index}&&movie_id=${movie_id}&&cinemanm=${cinemanm}&&hall_id=${hall_id}`,
    })
  }
  
})